package net.rrkk.photoshow.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ImageUtils {
    
    /**
     * 调整图片尺寸
     * 
     * @param originalImage 原始图片
     * @param width 目标宽度
     * @param height 目标高度
     * @return 调整尺寸后的图片
     */
    public static BufferedImage resizeImage(BufferedImage originalImage, int width, int height) {
        if (width <= 0 && height <= 0) {
            return originalImage;
        }
        
        int originalWidth = originalImage.getWidth();
        int originalHeight = originalImage.getHeight();
        
        // 如果只指定一个维度，按比例计算另一个维度
        if (width <= 0) {
            width = (int) Math.round((double) originalWidth / originalHeight * height);
        } else if (height <= 0) {
            height = (int) Math.round((double) originalHeight / originalWidth * width);
        }
        
        // 创建缩放后的图片
        Image scaledImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = resizedImage.createGraphics();
        g2d.drawImage(scaledImage, 0, 0, null);
        g2d.dispose();
        
        return resizedImage;
    }
    
    /**
     * 为图片添加水印
     * 
     * @param originalImage 原始图片
     * @param watermarkText 水印文字
     * @return 添加水印后的图片
     */
    public static BufferedImage addWatermark(BufferedImage originalImage, String watermarkText) {
        // 添加水印
        Graphics2D g2d = originalImage.createGraphics();
        AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
        g2d.setComposite(alphaChannel);
        
        // 设置水印字体和颜色
        g2d.setColor(Color.WHITE);
        g2d.setFont(new Font("Arial", Font.BOLD, 60));
        
        // 计算水印位置（右下角）
        FontMetrics fontMetrics = g2d.getFontMetrics();
        int textWidth = fontMetrics.stringWidth(watermarkText);
        int textHeight = fontMetrics.getHeight();
        
        int x = originalImage.getWidth() - textWidth - 20;
        int y = originalImage.getHeight() - 20;
        
        // 绘制水印文字
        g2d.drawString(watermarkText, x, y);
        g2d.dispose();
        
        return originalImage;
    }
    
    /**
     * 将BufferedImage转换为字节数组
     * 
     * @param image 图片
     * @param format 图片格式
     * @return 字节数组
     * @throws IOException IO异常
     */
    public static byte[] imageToByteArray(BufferedImage image, String format) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, format, baos);
        return baos.toByteArray();
    }
    
    /**
     * 根据MIME类型确定图片格式
     * 
     * @param mimeType MIME类型
     * @return 图片格式
     */
    public static String getImageFormat(String mimeType) {
        String format = "jpg";
        if (mimeType != null) {
            if (mimeType.contains("png")) {
                format = "png";
            } else if (mimeType.contains("gif")) {
                format = "gif";
            }
        }
        return format;
    }
}